// 社区相关类型定义

export interface CommunityShare {
  id: number;
  userId?: number; // 保持向后兼容
  user_id?: number; // API 格式
  user: {
    id: number;
    username: string;
    avatar?: string;
    avatar_url?: string; // API 格式
    tier?: string;
    level?: number; // 用户等级
  };
  title?: string;
  content: string;
  screenshot?: string;
  images?: Array<{
    // 图片数组
    id?: number;
    url: string;
    thumbnail_url?: string;
    alt_text?: string;
  }>;
  tags?: string[];
  category?: "success" | "experience" | "tip" | "question";
  share_type?: "achievement" | "experience" | "question"; // API 格式
  // 支持两种命名格式
  likesCount?: number; // 保持向后兼容
  likes_count?: number; // API 格式
  dislikesCount?: number; // 保持向后兼容
  dislikes_count?: number; // API 格式
  commentsCount?: number; // 保持向后兼容
  comments_count?: number; // API 格式
  shares_count?: number; // 分享数量
  viewsCount?: number; // 保持向后兼容
  views_count?: number; // API 格式
  isLiked?: boolean; // 保持向后兼容
  is_liked?: boolean; // API 格式
  isDisliked?: boolean; // 保持向后兼容
  is_disliked?: boolean; // API 格式
  is_bookmarked?: boolean; // 是否已收藏
  isFeatured?: boolean; // 保持向后兼容
  is_featured?: boolean; // API 格式
  isReported?: boolean; // 保持向后兼容
  is_reported?: boolean; // API 格式
  is_public?: boolean; // 是否公开
  createdAt?: string; // 保持向后兼容
  created_at?: string; // API 格式
  updatedAt?: string; // 保持向后兼容
  updated_at?: string; // API 格式
}

export interface HelpRequest {
  id: number;
  userId?: number; // 保持向后兼容
  user_id?: number; // API 格式
  user: {
    id: number;
    username: string;
    avatar?: string;
    avatar_url?: string; // API 格式
    tier?: string;
    level?: number; // 用户等级
  };
  title: string;
  content?: string; // 保持向后兼容
  description: string; // 组件中使用的属性
  category?: "technical" | "social" | "training" | "general";
  tags?: string[];
  status: "open" | "answered" | "solved" | "closed";
  isUrgent?: boolean; // 保持向后兼容
  is_urgent?: boolean; // API 格式
  answersCount?: number; // 保持向后兼容
  answers_count: number; // API 格式
  viewsCount?: number; // 保持向后兼容
  views_count: number; // API 格式
  bestAnswerId?: number; // 保持向后兼容
  best_answer_id?: number; // API 格式
  best_answer?: HelpAnswer; // 最佳答案对象
  bountyPoints?: number; // 保持向后兼容
  reward_points?: number; // API 格式
  is_bookmarked: boolean; // 是否已收藏
  createdAt?: string; // 保持向后兼容
  created_at: string; // API 格式
  updatedAt?: string; // 保持向后兼容
  updated_at?: string; // API 格式
}

export interface HelpAnswer {
  id: number;
  helpRequestId: number;
  userId: number;
  user: {
    id: number;
    username: string;
    avatar: string;
    tier: string;
  };
  content: string;
  upvotesCount: number;
  isUpvoted: boolean;
  isBestAnswer: boolean;
  isReported: boolean;
  createdAt: string;
  updatedAt: string;
}

export interface Comment {
  id: string;
  shareId?: number;
  userId?: number;
  user: {
    id: number;
    username: string;
    avatar: string;
  };
  content: string;
  parentId?: number;
  replies?: Comment[];
  likesCount: number;
  isLiked: boolean;
  createdAt: string;
  updatedAt?: string;
}

export interface CreateShareData {
  title?: string;
  content: string;
  screenshot?: File | string;
  tags: string[];
  category: "success" | "experience" | "tip" | "question";
}

export interface CreateHelpRequestData {
  title: string;
  content: string;
  category: "technical" | "social" | "training" | "general";
  tags: string[];
  isUrgent?: boolean;
  bountyPoints?: number;
}

export interface CreateHelpAnswerData {
  content: string;
}

export interface InteractionData {
  type: "like" | "dislike" | "comment";
  comment?: string;
}

export interface ContentFilter {
  page?: number;
  limit?: number;
  category?: string;
  tags?: string[];
  sortBy?: "newest" | "popular" | "trending" | "most_liked";
  sortOrder?: "asc" | "desc";
  search?: string;
  timeRange?: "today" | "week" | "month" | "all";
  status?: "open" | "answered" | "closed"; // for help requests
}

// User Daily Challenge (specific to community)
export interface UserDailyChallenge {
  challengeId: number;
  userId: number;
  challengeDate: string;
  isCompleted: boolean;
  completedAt?: string;
  progress: number;
  rewardEarned?: number;
}

export interface Achievement {
  id: number;
  title: string;
  name?: string; // 保持向后兼容
  description: string;
  icon?: string;
  icon_url?: string;
  color?: string;
  category?: "training" | "social" | "progress" | "special";
  category_id?: number;
  type?: "bronze" | "silver" | "gold" | "platinum";
  points: number;
  pointsReward?: number; // 保持向后兼容
  requirements?: string[];
  is_unlocked: boolean;
  isUnlocked?: boolean; // 保持向后兼容
  unlocked_at?: string;
  unlockedAt?: string; // 保持向后兼容
  progress?: {
    current: number;
    target: number;
  };
  rarity?: "common" | "rare" | "epic" | "legendary";
  is_featured?: boolean;
  badge_url?: string;
}

export interface UserNotification {
  id: number;
  userId: number;
  type: "achievement" | "challenge" | "social" | "training" | "system";
  title: string;
  content: string;
  data?: Record<string, any>;
  isRead: boolean;
  priority: "low" | "medium" | "high";
  createdAt: string;
  readAt?: string;
}

// Community Post types (using inline TrainingResult to avoid conflicts)
export interface CommunityPost {
  id: string | number;
  title?: string;
  content: string;
  type?: "highlight" | "help" | "question" | "tip";
  images?: string[];
  tags?: string[];
  user: {
    id: number;
    username: string;
    avatar?: string;
    avatar_url?: string; // API 格式
  };
  // 支持两种命名格式
  likesCount?: number; // 保持向后兼容
  likes_count?: number; // API 格式
  commentsCount?: number; // 保持向后兼容
  comments_count?: number; // API 格式
  shares_count?: number; // 分享数量
  isLiked?: boolean; // 保持向后兼容
  is_liked?: boolean; // API 格式
  isBookmarked?: boolean; // 保持向后兼容
  is_bookmarked?: boolean; // API 格式
  createdAt?: string; // 保持向后兼容
  created_at?: string; // API 格式
  latest_comments?: Array<{
    id: number;
    user: {
      id: number;
      username: string;
      avatar?: string;
    };
    content: string;
    created_at: string;
  }>;
  training_result?: {
    scenario_name: string;
    score: number;
    duration: number;
  };
  trainingResult?: {
    // 保持向后兼容
    id: number;
    scenarioTitle: string;
    score: number;
    duration: number;
    completedAt: string;
  };
}

// Community Group types
export interface CommunityGroup {
  id: number;
  name: string;
  description?: string;
  avatar?: string; // 群组头像
  coverImage?: string; // 群组封面（保持向后兼容）
  cover_image?: string; // API 返回格式
  membersCount?: number; // 保持向后兼容
  members_count: number; // API 返回格式
  postsCount?: number; // 保持向后兼容
  posts_count: number; // API 返回格式
  activity_level: "Low" | "Medium" | "High";
  isJoined?: boolean; // 保持向后兼容
  is_joined: boolean; // API 返回格式
  is_private: boolean;
  is_verified: boolean;
  tags?: string[];
  category_id?: number;
  latest_activity?: {
    text: string;
    time: string;
  };
  joining?: boolean; // 用于UI状态管理
  createdAt?: string; // 保持向后兼容
  created_at?: string; // API 返回格式
  updated_at?: string;
}
